package com.prs.crm.action.sys;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.XJAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.sys.Module;
import com.prs.crm.domain.sys.ToolUrl;
import com.prs.crm.domain.sys.UIResource;
import com.prs.crm.service.sys.ModuleService;
import com.prs.crm.service.sys.ToolUrlService;

public class ModuleAction extends XJAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3053434061685458805L;

	private Module module;

	private ModuleService moduleService;

	private PaginationSupport pagination;

	private UIResource resource;

	private Integer[] moduleIds;

	private TreeNode node;
	private ToolUrl toolUrl;
	private ToolUrlService toolUrlService;

	public ToolUrl getToolUrl() {
		return toolUrl;
	}

	public void setToolUrl(ToolUrl toolUrl) {
		this.toolUrl = toolUrl;
	}

	public ToolUrlService getToolUrlService() {
		return toolUrlService;
	}

	public void setToolUrlService(ToolUrlService toolUrlService) {
		this.toolUrlService = toolUrlService;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	public Module getModule() {
		return module;
	}

	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}

	public ModuleService getModuleService() {
		return moduleService;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setNode(TreeNode node) {
		this.node = node;
	}

	public TreeNode getNode() {
		return node;
	}

	public void setModuleIds(Integer[] moduleIds) {
		this.moduleIds = moduleIds;
	}

	public Integer[] getModuleIds() {
		return moduleIds;
	}

	public void setResource(UIResource resource) {
		this.resource = resource;
	}

	public UIResource getResource() {
		return resource;
	}

	@Action("listModule")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Module.class);
		this.setPagination(this.getModuleService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("selectModule")
	public String selectModule() {
		execute();
		return SUCCESS;
	}

	@Action(value = "saveModule", results = {
			@Result(name = "success", type = "redirect", location = "listModule"),
			@Result(name = "error", type = "chain", location = "editModule"),
			@Result(name = "input", type = "redirect", location = "editModule?module=${module.id}") })
	public String save() {
		if (module.getName() == null || "".equals(module.getName().trim())) {
			addActionError("名称不能为空");
			return ERROR;
		}
		
		boolean input = (module.getId() == null);
		this.getModuleService().save(module);
		return (input) ? INPUT : SUCCESS;
	}

	@Action(value = "saveResource", results = { @Result(name = "success", type = "redirect", location = "editModule?module=${module.id}") })
	public String saveResource() {
		
		this.getModuleService().saveResource(module, resource);
		resource = null;
		return SUCCESS;
	}

	@Action(value = "removeResource", results = { @Result(name = "success", type = "redirect", location = "editModule?module=${module.id}") })
	public String removeResource() {
		this.getModuleService().removeResource(resource);
		resource = null;
		return SUCCESS;
	}

	@Action(value = "setHomeResource", results = { @Result(name = "success", type = "redirect", location = "editModule?module=${module.id}") })
	public String setHomeResource() {
		this.getModuleService().setHome(resource);
		return SUCCESS;
	}

	@Action("editModule")
	public String edit() {
		return SUCCESS;
	}

	@Action(value = "removeModule", results = { @Result(name = "success", type = "redirect", location = "listModule") })
	public String remove() {
		this.getModuleService().batchRemove(moduleIds);
		return SUCCESS;
	}

	@Action(value = "loadModuleTree", results = { @Result(name = "success", type = "json", params = {
			"root", "node" }) })
	public String loadModuleTree() {
		node = new TreeNode();
		node.setId("-1");
		node.setText("");

		TreeNode root = new TreeNode();
		root.setId("0");
		root.setText("系统模块");
		node.addChildNode(root);

		Collection<Module> modules = this.getModuleService().getAllModule();
		for (Module module : modules) {
			TreeNode child = new TreeNode();
			child.setId(module.getId().toString());
			child.setText(module.getName());
			root.addChildNode(child);
		}
		return SUCCESS;
	}

}
